package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.behavior.mapper.ApBehaviorEntryMapper;
import com.usian.behavior.mapper.ApFollowBehaviorMapper;
import com.usian.behavior.service.ApBehaviorEntryService;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior>implements ApFollowBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Override
    public ResponseResult createFollowBehavior(FollowBehaviorDto dto) {
        //根据userId查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdAndEquipmentId(dto.getUserId(), null);
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(502,"行为实体不存在");
        }
        //创建关注行为对象并保存
        ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
        apFollowBehavior.setArticleId(dto.getArticleId());
        apFollowBehavior.setCreatedTime(new Date());
        apFollowBehavior.setEntryId(apBehaviorEntry.getId());
        apFollowBehavior.setFollowId(dto.getFollowId());
        save(apFollowBehavior);
        return ResponseResult.okResult(200,"关注成功");
    }
}
